package com.guohong.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guohong.dto.ElTreeDto;
import com.guohong.dto.RoleDto;
import com.guohong.dto.RoleOptionDto;
import com.guohong.dto.RolePageDto;
import com.guohong.entity.Role;
import com.guohong.entity.RoleMenu;
import com.guohong.entity.RolePermission;
import com.guohong.mapper.MenuMapper;
import com.guohong.mapper.RoleMapper;
import com.guohong.mapper.RoleMenuMapper;
import com.guohong.mapper.RolePermissionMapper;
import com.guohong.service.RoleService;
import com.guohong.vo.RoleAddVo;
import com.guohong.vo.RoleUpdateVo;

@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Autowired
	private RolePermissionMapper rolePermissionMapper;
	@Autowired
	private MenuMapper menuMapper;
	
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Transactional
	@Override
	public Role addRole(Role role) {
		roleMapper.insert(role);
		return role;
	}

	@Transactional
	@Override
	public RolePageDto listRolesByPage(String roleName, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<Role> roles = roleMapper.listRolesByPage(roleName);

		List<RoleDto> roleDtos = null;
		RolePageDto rolePageDto = null;

		if (!CollectionUtils.isEmpty(roles)) {
			roleDtos = new ArrayList<RoleDto>();
			rolePageDto = new RolePageDto();

			for (Role role : roles) {
				RoleDto roleDto = new RoleDto();
				BeanUtils.copyProperties(role, roleDto, "createTime", "updateTime");
				if (null != role.getCreateTime()) {
					roleDto.setCreateTime(formatter.format(role.getCreateTime()));
				}
				
				// 获取分配的权限（编辑时使用）
				List<String> menuNodekeys = menuMapper.getNodekeysByRole(role.getId());
				List<String> permiNodekeys = rolePermissionMapper.getNodekeysByRole(role.getId());
				menuNodekeys.addAll(permiNodekeys);
				roleDto.setPermis(menuNodekeys);
				
				roleDtos.add(roleDto);
			}

			rolePageDto.setTotal(roles.getTotal());
			rolePageDto.setRoles(roleDtos);
		}

		return rolePageDto;
	}

//	@Override
//	public void authorization(Long roleId, List<Menu> menus, List<MenuPermission> menuPermissions) {
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public List<RoleOptionDto> getRoleOptions() {
		List<Role> roles = roleMapper.list();
		List<RoleOptionDto> options = null;
		
		if (!CollectionUtils.isEmpty(roles)) {
			options = new ArrayList<>();
			
			for (Role role : roles) {
				RoleOptionDto roleOptionDto = new RoleOptionDto();
				roleOptionDto.setId(role.getId());
				roleOptionDto.setLabel(role.getRoleName());
				options.add(roleOptionDto);
			}
		}
		
		return options;
	}

	@Transactional
	@Override
	public int addRole(RoleAddVo roleAddVo) {
		Role role = new Role();
		role.setRoleCode(roleAddVo.getRoleCode());
		role.setRoleName(roleAddVo.getRoleName());
		role.setDescr(roleAddVo.getDescr());
		role.setCreateTime(new Date());
		// TODO
		role.setCreateUser("admin");
		// insert
		int result = roleMapper.insert(role);

		List<ElTreeDto> elTreeDtos = roleAddVo.getMenus();
		for (ElTreeDto elTreeDto : elTreeDtos) {
			if (elTreeDto.getIsMenu()) { // 是菜单
				RoleMenu roleMenu = new RoleMenu();
				roleMenu.setRoleId(role.getId());
				roleMenu.setMenuId(elTreeDto.getId());
				roleMenuMapper.insert(roleMenu);
			} else { // 是权限
				RolePermission rolePermission = new RolePermission();
				rolePermission.setRoleId(role.getId());
				rolePermission.setPermissionId(elTreeDto.getId());
				rolePermissionMapper.insert(rolePermission);
			}
		}

		return result;
	}

	@Transactional
	@Override
	public int updateRole(RoleUpdateVo roleUpdateVo) {
		Role oldRole = roleMapper.selectByPrimaryKey(roleUpdateVo.getId());
		
		oldRole.setRoleCode(roleUpdateVo.getRoleCode());
		oldRole.setRoleName(roleUpdateVo.getRoleName());
		oldRole.setDescr(roleUpdateVo.getDescr());
		// update
		int result = roleMapper.updateByPrimaryKey(oldRole);
		
		// 先删除调关联
		roleMenuMapper.deleteByRoleId(oldRole.getId());
		rolePermissionMapper.deleteByRoleId(oldRole.getId());

		List<ElTreeDto> elTreeDtos = roleUpdateVo.getMenus();
		for (ElTreeDto elTreeDto : elTreeDtos) {
			if (elTreeDto.getIsMenu()) { // 是菜单
				RoleMenu roleMenu = new RoleMenu();
				roleMenu.setRoleId(oldRole.getId());
				roleMenu.setMenuId(elTreeDto.getId());
				roleMenuMapper.insert(roleMenu);
			} else { // 是权限
				RolePermission rolePermission = new RolePermission();
				rolePermission.setRoleId(oldRole.getId());
				rolePermission.setPermissionId(elTreeDto.getId());
				rolePermissionMapper.insert(rolePermission);
			}
		}

		return result;
	}

}
